package com.kedacom.ctsp.authority.util;

import com.kedacom.ctsp.lang.mapper.BeanMapper;
import com.kedacom.ctsp.web.entity.TreeSortSupportBean;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AuthorityTreeUtil {

    public static <T extends TreeSortSupportBean, V extends TreeSortSupportBean> void addParent(T t, V v) {
        // 设置parent
        if (v.getParent() != null && v.getParent().getId() != null) {
            TreeSortSupportBean<T, String> parent = BeanMapper.newInstanceSilently(t.getClass());
            parent.setId(String.valueOf(v.getParent().getId()));

            log.info("设置parent: [{}]", parent.getId());
            t.setParent(parent);
        }
    }

    public static <T extends TreeSortSupportBean> void updateSetLevel(T t) {
        log.info("设置level, 当前level :[{}]", t.getLevel());
        if (t.getParent() == null) {
            log.info("如果没有填写parent, 则level设置默认第一层");
            t.setLevel(1);
            return;
        }
        t.setLevel(t.getLevel() + 1);
        log.info("设置了parent,  则level设置为parent+1 :[{}]", t.getLevel());
    }
}
